import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class Testresourcequota:
    url_unify = "/clusters/host"

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.resourcequota
    @pytest.mark.parametrize('resourcequotainfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/resourcequota/1_get_resourcequota_list.yaml'))
    def test_get_resourcequota_list(self, resourcequotainfo):
        # 获取resourcequota列表接口-正常
        base_url = datas.baseUrl + Testresourcequota.url_unify
        url = base_url + resourcequotainfo['request']['url'].replace('{namespace}', 'default')
        method = resourcequotainfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == resourcequotainfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.resourcequota
    @pytest.mark.parametrize('resourcequotainfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/resourcequota/2_create_resourcequota.yaml'))
    def test_create_resourcequota(self, resourcequotainfo):
        # 创建resourcequota接口-正常
        base_url = datas.baseUrl + Testresourcequota.url_unify
        url = base_url + resourcequotainfo['request']['url'].replace('{namespace}', 'default')
        method = resourcequotainfo['request']['method']
        data = resourcequotainfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        resourcequotaname = req.json()['metadata']['name']
        globals()['resourcequotaname'] = resourcequotaname
        assert req.status_code == resourcequotainfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.resourcequota
    @pytest.mark.parametrize('resourcequotainfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/resourcequota/3_get_specific_resourcequota.yaml'))
    def test_get_specific_resourcequota(self, resourcequotainfo):
        # 获取指定resourcequota接口-正常
        base_url = datas.baseUrl + Testresourcequota.url_unify
        url = base_url + resourcequotainfo['request']['url'].replace('{namespace}', 'default').replace(
            '{resourcequotaname}', globals()['resourcequotaname'])
        method = resourcequotainfo['request']['method']
        req = RequestUtil().send_request(method, url)
        updatereqbody = req.json()
        globals()['updatereqbody'] = updatereqbody
        assert req.status_code == resourcequotainfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.resourcequota
    @pytest.mark.parametrize('resourcequotainfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/resourcequota/4_update_resourcequota.yaml'))
    def test_update_resourcequota(self, resourcequotainfo):
        # 更新指定resourcequota接口-正常
        base_url = datas.baseUrl + Testresourcequota.url_unify
        url = base_url + resourcequotainfo['request']['url'].replace('{namespace}', 'default').replace(
            '{resourcequotaname}', globals()['resourcequotaname'])
        method = resourcequotainfo['request']['method']
        resourcequotainfo['request']['data'] = globals()['updatereqbody']
        data = resourcequotainfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == resourcequotainfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.resourcequota
    @pytest.mark.parametrize('resourcequotainfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/resourcequota/5_update_section_resourcequota.yaml'))
    def test_update_section_resourcequota(self, resourcequotainfo):
        # 部分更新指定resourcequota的接口-正常
        base_url = datas.baseUrl + Testresourcequota.url_unify
        url = base_url + resourcequotainfo['request']['url'].replace('{namespace}', 'default').replace(
            '{resourcequotaname}', globals()['resourcequotaname'])
        method = resourcequotainfo['request']['method']
        data = resourcequotainfo['request']['data']
        header = resourcequotainfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == resourcequotainfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.resourcequota
    @pytest.mark.parametrize('resourcequotainfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/resourcequota/6_delete_specific_resourcequota.yaml'))
    def test_delete_specific_resourcequota(self, resourcequotainfo):
        # 删除resourcequota接口-正常
        base_url = datas.baseUrl + Testresourcequota.url_unify
        url = base_url + resourcequotainfo['request']['url'].replace('{namespace}', 'default').replace(
            '{resourcequotaname}', globals()['resourcequotaname'])
        method = resourcequotainfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == resourcequotainfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.resourcequota
    @pytest.mark.parametrize('resourcequotainfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/resourcequota/1_get_resourcequota_list_exception.yaml'))
    def test_get_resourcequota_list_exception(self, resourcequotainfo):
        # 获取resourcequota列表接口-异常
        base_url = datas.baseUrl + Testresourcequota.url_unify
        url = base_url + resourcequotainfo['request']['url'].replace('{namespace}', 'default')
        method = resourcequotainfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == resourcequotainfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.resourcequota
    @pytest.mark.parametrize('resourcequotainfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/resourcequota/2_create_resourcequota_exception.yaml'))
    def test_create_resourcequota_exception(self, resourcequotainfo):
        # 创建resourcequota接口-异常
        base_url = datas.baseUrl + Testresourcequota.url_unify
        url = base_url + resourcequotainfo['request']['url'].replace('{namespace}', 'default')
        method = resourcequotainfo['request']['method']
        data = resourcequotainfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == resourcequotainfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.resourcequota
    @pytest.mark.parametrize('resourcequotainfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/resourcequota/3_get_specific_resourcequota_exception.yaml'))
    def test_get_specific_resourcequota_exception(self, resourcequotainfo):
        # 获取指定resourcequota接口-异常
        base_url = datas.baseUrl + Testresourcequota.url_unify
        url = base_url + resourcequotainfo['request']['url'].replace('{namespace}', 'default').replace(
            '{resourcequotaname}', globals()['resourcequotaname'])
        method = resourcequotainfo['request']['method']
        req = RequestUtil().send_request(method, url)
        updatereqbody = req.json()
        globals()['updatereqbody'] = updatereqbody
        assert req.status_code == resourcequotainfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.resourcequota
    @pytest.mark.parametrize('resourcequotainfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/resourcequota/4_update_resourcequota_exception.yaml'))
    def test_update_resourcequota_exception(self, resourcequotainfo):
        # 更新指定resourcequota接口-异常
        base_url = datas.baseUrl + Testresourcequota.url_unify
        url = base_url + resourcequotainfo['request']['url'].replace('{namespace}', 'default').replace(
            '{resourcequotaname}', globals()['resourcequotaname'])
        method = resourcequotainfo['request']['method']
        resourcequotainfo['request']['data'] = globals()['updatereqbody']
        data = resourcequotainfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == resourcequotainfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.resourcequota
    @pytest.mark.parametrize('resourcequotainfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/resourcequota/5_update_section_resourcequota_exception.yaml'))
    def test_update_section_resourcequota_exception(self, resourcequotainfo):
        # 部分更新指定resourcequota的接口-异常
        base_url = datas.baseUrl + Testresourcequota.url_unify
        url = base_url + resourcequotainfo['request']['url'].replace('{namespace}', 'default').replace(
            '{resourcequotaname}', globals()['resourcequotaname'])
        method = resourcequotainfo['request']['method']
        data = resourcequotainfo['request']['data']
        header = resourcequotainfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == resourcequotainfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.resourcequota
    @pytest.mark.parametrize('resourcequotainfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/resourcequota/6_delete_specific_resourcequota_exception.yaml'))
    def test_delete_specific_resourcequota_exception(self, resourcequotainfo):
        # 删除resourcequota接口-异常
        base_url = datas.baseUrl + Testresourcequota.url_unify
        url = base_url + resourcequotainfo['request']['url'].replace('{namespace}', 'default').replace(
            '{resourcequotaname}', globals()['resourcequotaname'])
        method = resourcequotainfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == resourcequotainfo['validate'][0]['equals']['status_code']
